package com.atguigu.lease.web.admin.controller.apartment;


import com.atguigu.lease.common.result.Result;
import com.atguigu.lease.model.entity.LabelInfo;
import com.atguigu.lease.model.enums.ItemType;
import com.atguigu.lease.web.admin.service.LabelInfoService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Tag(name = "标签管理")
@RestController
@RequestMapping("/admin/label")
public class LabelController {

    @Autowired
    private LabelInfoService labelInfoService;

    /**
     * 调用查询：
     * 1. list -> 查询所有 -》 type = null select * from label_info
     * 2. list -> type = 2 -> 查询房间 -> select * from label_info where type = 2
     * 3. list -> type = 1 -> 查询公寓 -> select * from label_info where type = 1
     * <p>
     * 错误：前端根据类型查询：400错误
     * 后端使用枚举接收，前端使用数字进行传递，参数不匹配
     *
     * 解决：配置三个转换器
     * springmvc converter
     * jackson @JsonValue
     * mybatis | mybatisPlus @EnumValue
     *
     * @param type
     * @return
     */
    @Operation(summary = "（根据类型）查询标签列表")
    @GetMapping("/list")
    public Result<List<LabelInfo>> labelList(@RequestParam(required = false) ItemType type) {
        LambdaQueryWrapper<LabelInfo> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(type != null, LabelInfo::getType, type);
        List<LabelInfo> labelInfoList = labelInfoService.list(queryWrapper);
        return Result.ok(labelInfoList);
    }

    @Operation(summary = "新增或修改标签信息")
    @PostMapping("/saveOrUpdate")
    public Result saveOrUpdateLabel(@RequestBody LabelInfo labelInfo) {
        boolean saveOrUpdate = labelInfoService.saveOrUpdate(labelInfo);
        return saveOrUpdate ? Result.ok() : Result.fail();
    }

    @Operation(summary = "根据id删除标签信息")
    @DeleteMapping("/deleteById")
    public Result deleteLabelById(@RequestParam Long id) {
        labelInfoService.removeById(id);
        return Result.ok();
    }
}
